package zuosheng.string;

/**
 * @author lj
 * @Description: 找到字符串的最长无重复字符子串
 * @date 2020-11-15 13:54
 * @QQ 851477108
 */


public class MaxUnique {
    public static void main(String[] args) {
        String str = "aabcb";
        System.out.println(maxUnique(str));
    }

    public static int maxUnique(String s){
        if (s == null || s.isEmpty()){
            return 0;
        }
        char[] chars = s.toCharArray();
        int[] map = new int[256];
        for (int i = 0; i < 256; i++){
            map[i] = -1;
        }
        int len = 0;
        int pre = -1;
        int cur = 0;
        for (int i = 0; i != chars.length; i++){
            pre = Math.max(pre, map[chars[i]]);
            cur = i - pre;
            len = Math.max(len , cur);
            map[chars[i]] = i;
        }
        return len;
    }
}
